/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dbdir.core.xml;

import java.io.File;
import java.io.IOException;
import org.dbdir.core.Logger;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Owner
 */
public class XMLParser { //Parses XML file for config and cache datastore

    private Logger logger = new Logger();

    public XMLParser() {
    }

    public NodeList doParse(String xmlFilePath, String targetElement) {
        NodeList nodes = null;
        try {
            try {
                File xmlFile = new File(xmlFilePath);
                //InputSource inputSrc = new InputSource();
                Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlFile.getAbsolutePath());
                nodes = (NodeList) doc.getDocumentElement().getElementsByTagName(targetElement);
            } catch (SAXException ex) {
                logger.log(2, ex.getMessage());
            } catch (IOException ex) {
                logger.log(2, ex.getMessage());
            }
        } catch (ParserConfigurationException ex) {
            logger.log(2, ex.getMessage());
        }
        return nodes;
    }

    public NodeList doParse(String xmlFilePath) {
        NodeList nodes = null;
        try {
            File xmlFile = new File(xmlFilePath);
            Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlFile.getAbsolutePath());
            Node docNode = doc.getDocumentElement();
            nodes = docNode.getChildNodes();
        } catch (SAXException ex) {
            logger.log(2, ex.getMessage());
        } catch (IOException ex) {
            logger.log(2, ex.getMessage());
        } catch (ParserConfigurationException ex) {
            logger.log(2, ex.getMessage());
        }
        return nodes;
    }
}
